import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';

class ImageBuilder {
  static Widget build({
    String? imageUrl,
    required ImageProvider placeholderImage,
    PlaceholderWidgetBuilder? placeholder,
    LoadingErrorWidgetBuilder? errorWidget,
    Color? color,
    double? width,
    double? height,
    BoxFit? fit,
  }) {
    Image image = Image(
      image: placeholderImage,
      width: width,
      height: height,
      fit: fit,
    );

    return imageUrl != null && imageUrl.isNotEmpty
        ? CachedNetworkImage(
            imageUrl: imageUrl,
            width: width,
            height: height,
            fit: fit,
            placeholder: (context, url) =>
                placeholder?.call(context, url) ?? image,
            errorWidget: (context, url, error) =>
                errorWidget?.call(context, url, error) ?? image,
            color: color,
            fadeOutDuration: const Duration(milliseconds: 0),
            fadeInDuration: const Duration(milliseconds: 0),
          )
        : image;
  }
}
